<?php

namespace App\Controllers\Lessions;

use App\Models\Lession\CommentCopy;
/**

 */
class GetLessionComment extends \App\Libs\Base\BaseAction
{

    public function execute()
    {
        if(!$this->request->hasQuery('lession_id')){
            return \App\Libs\Views\Json::view(1, '获取哪节课的评论？', []);
        }
        $lessionID = $this->request->getQuery('lession_id');
        $page = $this->request->getQuery('page','int',1);
        $num = $this->request->getQuery('num','int',10);
        $offset = ($page-1)*$num;
        if(!$this->request->hasQuery('comment_id')){
            $allComment = CommentCopy::findAll([
                'conditions' => 'lession_id=?1 and fid=0',
                'bind' =>[1=>$lessionID],
                'offset' =>$offset,
                'limit' => $num
            ]);
            $allComment->rewind();
            if($allComment->valid()) {
                while ($allComment->valid()) {
                    $current = $allComment->current();//当前评论时顶级评论;

                    $allChildComment = CommentCopy::count([
                        'conditions' => 'lession_id=?1 and fid=?2',
                        'bind' => [1 => $lessionID, 2 => $current->id],
                    ]);

                    $topComment = $current->toArray(['id', 'from_uid', 'lession_id', 'content', 'fid', 'ctime']);
                    $topComment['from_username'] = $current->getSingleUserName($topComment['from_uid']);
                    $topComment['reply_num'] = $allChildComment;
                    $allTopComment[] = $topComment;
                    $allComment->next();
                }
                return \App\Libs\Views\Json::view(0, '成功', $allTopComment);
            }else{
                return \App\Libs\Views\Json::view(1, '还没有评论，来一评先？', []);

            }
        }else{
                $commentID = $this->request->getQuery('comment_id');
                $allChildComment = CommentCopy::findAll([
                    'conditions' =>'lession_id=?1 and fid=?2',
                    'bind' => [1=>$lessionID,2=>$commentID],
                    'offset'=> $offset,
                    'limit'=> $num
                ]);
                $allChildComment->rewind();
                if($allChildComment->valid()){
                    while($allChildComment->valid()){
                        $currentChildComment = $allChildComment->current();
                        $singleChildComment = $currentChildComment->toArray();
                        $doubleUserName = $currentChildComment->getDoubleUserName($currentChildComment);
                        $singleChildComment['from_username'] = $doubleUserName[$singleChildComment['from_uid']];
                        $singleChildComment['to_username'] = $doubleUserName[$singleChildComment['to_uid']];
                        $childComment[] = $singleChildComment;
                        $allChildComment->next();
                    }
                    return \App\Libs\Views\Json::view(0, 'ok', $childComment);
                }else{
                    return \App\Libs\Views\Json::view(1, '没有评论了', []);
                }
        }
    }
}
